package com.kaiyidiannao.model.req.model;

import android.text.TextUtils;
import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.req.base.BaseCommand;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 *  凑队去（召集发布，微记发布）post 请求数据
 */
public class PostTourPublish extends BaseCommand {

    public static final String ID_CATID_GETHER = 53 +"";
    public static final String ID_CATID_SMALL_NOTE = 54 + "";

    private String TAG = "PostTourPublish";
    private String userid;
    private String catid; //53, 54
    private String title;
    private String introduce;
    private List<String> thumbs;

    // 凑对去 - 召集发布用
    private String fromtime; //开始时间
    private String totime; //结束时间
    private String people;
    private String cost;
    private String truename;
    private String telephone;


    /**
     * 微记发布页面
     */
    public PostTourPublish(String userid, String catid, String title, String introduce) {
        this.userid = userid;
        this.catid = catid;
        this.title = title;
        this.introduce = introduce;
    }

    /**
     *  召集发布页面
     */
    public PostTourPublish(String userid, String catid, String title, String fromtime, String totime,
                           String people, String cost, String truename, String telephone, String introduce) {
        this.userid = userid;
        this.catid = catid;
        this.title = title;
        this.fromtime = fromtime;
        this.totime = totime;
        this.people = people;
        this.cost = cost;
        this.truename = truename;
        this.telephone = telephone;
        this.introduce = introduce;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.POST_TOUT_publish_ADDRESS_;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("action","add");
        paramters.put("mid","31");
        paramters.put("userid", userid);
        paramters.put("post[catid]",catid);
        paramters.put("post[introduce]", introduce);
        paramters.put("post[title]", title);


        if (TextUtils.isEmpty(fromtime)) {
            paramters.put("post[fromtime]",fromtime);
        }
        if (TextUtils.isEmpty(totime)) {
            paramters.put("post[totime]",totime);
        }
        if (TextUtils.isEmpty(people)) {
            paramters.put("post[people]",people);
        }
        if (TextUtils.isEmpty(cost)) {
            paramters.put("post[cost]",cost);
        }
        if (TextUtils.isEmpty(truename)) {
            paramters.put("post[truename]",truename);
        }
        if (TextUtils.isEmpty(telephone)) {
            paramters.put("post[telephone]",telephone);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "PostTourPublish.yes()");
        DataCenter.tourPublishFlag = false;
        try {
            JSONObject jsonObject = new JSONObject(result.object.toString());
            String code = jsonObject.optString("code");
            if (code.equals(DataCenter.successFlag)) {
                DataCenter.tourPublishFlag = true;
            } else {
                DataCenter.tourPublishFlag = false;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        DataCenter.getInstance().callBacks(DataCenter.flag.POST_TOUR_PUBLISH_SUCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_INFO, "PostTourPublish.no()");
        DataCenter.tourPublishFlag = false;
        DataCenter.getInstance().callBacks(DataCenter.flag.POST_TOUR_PUBLISH_FAILED);
    }
}
